package com.guang.executor.executorinterupt;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author lg
 * @Description
 * @date 2022/10/28 10:33
 */
public class ExecutorShutdownTest {
    private final static Logger logger = LoggerFactory.getLogger(ExecutorShutdownTest.class);
    private static final ExecutorService executorService = new ThreadPoolExecutor(1,1,1, TimeUnit.MINUTES,new ArrayBlockingQueue<Runnable>(2));

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 3; i++) {
            executorService.execute(()->{
                Thread thread = Thread.currentThread();
                logger.info("当前线程正在执行任务，当前线程id名称是：{}",thread.getName());
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        // 返回的是队列中的任务
        executorService.shutdown();
        while (!executorService.isTerminated()){
            executorService.awaitTermination(500,TimeUnit.MILLISECONDS);
            logger.info("当前线程正在被终止");
        }
        logger.info("线程池终止了");
    }
}
